#include "Player.h"

#include "GamePlayState.h"

//Handling collisions
#include "Physics.h"
#include "Camera.h"
#include "Game.h"
#include "Time.h"
#include "ProfileManager.h"
#include "BitmapFont.h"
#include "AudioManager.h"

#include"..//SGD Wrappers/CSGD_Direct3D.h"
#include "../SGD Wrappers/CSGD_DirectInput.h"

//Collision checks
#include "MovingPlatform.h"
#include "ConveyorBelt.h"
#include "MetalBox.h"
#include "WoodBox.h"
#include "RadiationBarrel.h"

#include "Events/Event.h"


//Arms
#include "Arm.h"

Player::Player(void)
{
	TinyXMLDan tempLoader;
	//IdolAnim = tempLoader.Load("Test");

	RunningAnim = tempLoader.Load("XML/RobotMoving.xml");
	IdolAnim = tempLoader.Load("XML/IdleRobot.xml");
	RepairAnim = tempLoader.Load("XML/Repair.xml");
	DeathAnim = tempLoader.Load("XML/DeathAnim.xml");

	AnimationFacingLeft = false;

	ActiveAnimation = &IdolAnim; 

	this->positionX = 400;
	this->positionY = 100;
	IdolAnim.Play(true);

	velX = 0.0f;
	velY = 0.0f;
	curTime = 0.0f;
	isGrounded = false;
	isOver = false;
	areControlsLocked = false;
	canMoveLeft = true;
	canMoveRight = true;
	canMoveUp = true;
	canMoveDown = true;
	groundedDelay = 0.0f;
	RepairAnimationPlaying = false;
	DeathAnimationPlaying = false;
	RepairAnimationTimer = 0.0f;
	DeathAnimationTimer = 0.0f;
	widthSolid = 85;
	heightSolid = 115;
	ImmuneTimer = 0.0f;
	Immune = false;

	heading.fX = -1;
	heading.fY = 0;

	hud = new HUD(this);

	//Create Thermo Emitters
	EmitterManager::GetInstance()->Load("thermo_fire", "thermo_fire.xml", false, positionX, positionY);
	EmitterManager::GetInstance()->Load("thermo_ice", "thermo_ice.xml", false, positionX, positionY);
	EmitterManager::GetInstance()->Load("thermo_spark", "spark.xml", false, positionX, positionY);

	SetTag("Player");
	objectID = "Player";

	frontArm = new Arm(this, false);
	RearArm = new Arm(this, true);

	toolManager = new ToolManager(this);
}

Player::~Player(void)
{
	delete hud;
	delete frontArm;
	delete RearArm;
	delete toolManager;
}

void Player::Update(float deltaTime)
{

	// update the player's current time for the level
	curTime += deltaTime;


	//Reset the player if we've gone WAY too far
	if(positionY > 4000.0f)
	{
		ResetToCheckPoint();
	}

	//Update the immune timer
	if( ImmuneTimer > 0.0f)
	{
		ImmuneTimer-= deltaTime;
		if( ImmuneTimer <= 0.0f )
		{
			ImmuneTimer = 0.0f;
			Immune = false;
		}
	}

	//Update the Repair Anim Timer
	if( RepairAnimationPlaying == true )
	{
		RepairAnimationTimer -= deltaTime;
		if( RepairAnimationTimer <= 0.0f )
		{
			RepairAnimationPlaying = false;
			ChangeAnimation(IdolAnim);
		}
	}

	//The player has died. Reset after the anim has ended
	if( DeathAnimationPlaying == true )
	{
		DeathAnimationTimer -= deltaTime;
		if( DeathAnimationTimer <= 0.0f )
		{
			DeathAnimationTimer = 0.0f;
			ChangeAnimation(IdolAnim);
			ResetToCheckPoint();
			ActivateMovement();
			DeathAnimationPlaying = false;
		}
	}

	//update delay between ground
	groundedDelay -= deltaTime;

	//INPUT:
	HandleInput(deltaTime);

	//Account for gravity:
	if(!isGrounded)
	{
		velY = Physics::GetGravity();
	}
	else
	{
		velY = 0.0f;
	}

	//Update Position when not locked
	if(RepairAnimationPlaying == false && DeathAnimationPlaying == false )
	{
		positionX += velX * deltaTime;
		positionY += velY * deltaTime;
	}

	//Update Tools
	toolManager->Update(deltaTime);
	hud->Update(deltaTime);
	//update thermo emitters
	toolManager->SetToolHeading(THERMO,heading);
	/*{
	float tempEmitterPosX = positionX, tempEmitterPosY = positionY;
	if(heading.fY == 0)
	{
	if(heading.fX == 1)
	tempEmitterPosX = positionX - 45.0f;
	if(heading.fX == -1)
	tempEmitterPosX = positionX + 45.0f;
	}
	else if(heading.fY == 1)
	{
	tempEmitterPosY = positionY - 35.0f;
	}
	else if(heading.fY == -1)
	{
	tempEmitterPosY = positionY + 35.0f;
	}
	EmitterManager::GetInstance()->GetEmitter("thermo_fire")->SetPosX(tempEmitterPosX);
	EmitterManager::GetInstance()->GetEmitter("thermo_fire")->SetPosY(tempEmitterPosY);
	EmitterManager::GetInstance()->GetEmitter("thermo_ice")->SetPosX(tempEmitterPosX);
	EmitterManager::GetInstance()->GetEmitter("thermo_ice")->SetPosY(tempEmitterPosY);
	EmitterManager::GetInstance()->GetEmitter("thermo_spark")->SetPosX(tempEmitterPosX);
	EmitterManager::GetInstance()->GetEmitter("thermo_spark")->SetPosY(tempEmitterPosY);

	}*/
	//Assume that the player is falling every 0.5 seconds 
	if(groundedDelay <= 0.0f)
	{
		groundedDelay = 0.2f;
	}
	//reset canmove
	canMoveLeft = true;
	canMoveRight = true;
	canMoveDown = true;
	canMoveUp = true;

	//Check below the player. If there is nothing below the player apply gravity
	float PlayerHeight = (float)hitBox.bottom - hitBox.top;
	float PlayerWidth = (float)hitBox.right - hitBox.left; 

	//Calculate Center
	tVector2D PlayerCenter = { (PlayerWidth / 2) + hitBox.left, (PlayerHeight / 2) + hitBox.top };

	//Direction 
	tVector2D CheckBottomCastDirection = { 0, 1 };

	//Info to fill out
	LinecastInfo CheckBottomInfo;


	isGrounded = false;


	//if(Time::GetInstance().TotalFrames() % 10 == 0)
	//	isGrounded = false;


	if( ActiveAnimation != nullptr )
	{
		//Update the active animation heading
		if(AnimationFacingLeft == true )
			ActiveAnimation->FlipTheImage(true);
		else
			ActiveAnimation->FlipTheImage(false);

		ActiveAnimation->Update( deltaTime );

		/*RECT CalColRect = ActiveAnimation->GetCollisionBox();


		//Get Width and Height
		int CalColRectWidth = CalColRect.right - CalColRect.left;
		int CalColRectHeight = CalColRect.bottom - CalColRect.top;

		//Calc Cur hit box
		CalColRect.left = (LONG)this->positionX;
		CalColRect.top = (LONG)this->positionY;
		CalColRect.right = CalColRect.left + CalColRectWidth;
		CalColRect.bottom = CalColRect.top + CalColRectHeight;

		////Offset by the Anchor Point
		//POINT tempAnch = IdolAnim.GetCurrAnchorPoint();
		//CalColRect.left -= tempAnch.x - CalColRectWidth / 2;
		//CalColRect.right  -= tempAnch.x - CalColRectWidth / 2;
		//CalColRect.top -= tempAnch.y - CalColRectHeight / 2;
		//CalColRect.bottom -= tempAnch.y - CalColRectHeight / 2;

		POINT tempAnch = IdolAnim.GetCurrAnchorPoint();
		CalColRect.left -= CalColRectWidth / 2;
		CalColRect.right  -=  CalColRectWidth / 2;
		CalColRect.top -=  CalColRectHeight / 2;
		CalColRect.bottom -=  CalColRectHeight / 2;*/

		hitBox.left = (long)positionX - (widthSolid >> 1);
		hitBox.right = (long)(positionX + widthSolid) - (widthSolid >> 1);
		hitBox.top = (long)positionY - (heightSolid >> 1);
		hitBox.bottom = (long)(positionY + heightSolid) - (heightSolid >> 1) + 10;

#pragma region UNUSED CODE
		/*
		//Test Solid Col Box
		RECT SolidColRect = { (long)positionX, (long)positionY, (long)positionX + widthSolid, (long)positionY + heightSolid };

		SolidColRect.left -= widthSolid / 2;
		SolidColRect.right  -=  widthSolid / 2;
		SolidColRect.top -=  heightSolid / 2;
		SolidColRect.bottom -=  heightSolid / 2;

		SetHitBox(SolidColRect);
		*/
#pragma endregion
	}
	//Update Arms
	frontArm->Update(deltaTime);
	RearArm->Update(deltaTime);
}

void Player::Render(void) const
{

	if( ActiveAnimation == &RepairAnim )
	{
		//Make sure the repair animation cannot be flipped
		if(ActiveAnimation->GetIsFlipped() == true)
			ActiveAnimation->FlipTheImage(false);
	}
	if( ActiveAnimation != &DeathAnim )
	{
		RearArm->Render();
		toolManager->RenderRear();
	}

	if(ActiveAnimation != nullptr )
	{
		if( ActiveAnimation == &DeathAnim )
		{
			ActiveAnimation->Render((int)(this->positionX - Camera::GetInstance()->OffsetX()),
				(int)(this->positionY - Camera::GetInstance()->OffsetY() + 35));
		}
		else if(FloatEquals(isCrushing, 0) && AnimationFacingLeft == false && ActiveAnimation != &RepairAnim)
		{
			ActiveAnimation->Render((int)(this->positionX - Camera::GetInstance()->OffsetX()),
				(int)(this->positionY - Camera::GetInstance()->OffsetY()));
		}
		else if( AnimationFacingLeft == true && ActiveAnimation != &RepairAnim )
		{
			//Calculate the offset
			int LeftOffsetWidth = this->hitBox.right - hitBox.left;

			//Offset to adjust anchor
			LeftOffsetWidth += 15;

			ActiveAnimation->Render((int)(this->positionX - Camera::GetInstance()->OffsetX() + LeftOffsetWidth),
				(int)(this->positionY - Camera::GetInstance()->OffsetY()));
		}
		else if( AnimationFacingLeft == true && ActiveAnimation == &RepairAnim )
		{
			ActiveAnimation->Render((int)(this->positionX - Camera::GetInstance()->OffsetX()),
				(int)(this->positionY - Camera::GetInstance()->OffsetY() - 35),
				1.0f, 1.0f);
		}
		else if( AnimationFacingLeft == false && ActiveAnimation == &RepairAnim )
		{
			ActiveAnimation->Render((int)(this->positionX - Camera::GetInstance()->OffsetX()),
				(int)(this->positionY - Camera::GetInstance()->OffsetY() - 35),
				1.0f, 1.0f);
		}
		else
		{
			ActiveAnimation->Render((int)(this->positionX - Camera::GetInstance()->OffsetX()),
				(int)(this->positionY - Camera::GetInstance()->OffsetY()),
				1.0f, isCrushing);
		}
	}

	if(ActiveAnimation != &RepairAnim && ActiveAnimation != &DeathAnim )
	{
		frontArm->Render();
		toolManager->RenderFront();
	}


	toolManager->RenderFront();
	hud->Render();
	RECT box = GetHitBox();
	box.left = (int)(box.left - Camera::GetInstance()->OffsetX());
	box.right = (int)(box.right - Camera::GetInstance()->OffsetX());
	box.bottom = (int)(box.bottom - Camera::GetInstance()->OffsetY());
	box.top = (int)(box.top - Camera::GetInstance()->OffsetY());
	//CSGD_Direct3D::GetInstance()->DrawHollowRect(box,D3DCOLOR_ARGB(255,255,255,0));
}

void Player::HandleCollision( GameObject& colliding, CollisionInfo& other)
{
	if(!colliding.GetIsActive())
	{
		return;
	}
	if(colliding.GetObjectID() == "Generator")
		return;


	if( toolManager->GetGrap().GetAttached() && colliding.GetTag() != "grapple" && colliding.GetTag() != "exit door" && colliding.GetTag() != "enter door"
		&& colliding.GetObjectID() != "Machine" && colliding.GetObjectID() != "Generator")
	{
		toolManager->ToggleTool(GRAPPLE);
	}

	if(colliding.GetTag() != "grapple" )
	{
		switch(other.Side)
		{
		case TOP:
			canMoveUp = false;
			toolManager->GetGrap().SetDifY(toolManager->GetGrap().GetDifY() + 3 );
			break;
		case BOTTOM:
			canMoveDown = false;
			toolManager->GetGrap().SetDifY(toolManager->GetGrap().GetDifY() - 3 );
			break;
		}
	}

	if((toolManager->GetMag().GetAttched()) == &colliding)
	{
		//if(heading.fY != 0)
			return;
	}
	if(colliding.GetTag() == "enter door")
		return;


	//Check machine
	if( colliding.GetObjectID() == "Machine")
	{
		if( RepairAnimationPlaying == false && (CSGD_DirectInput::GetInstance()->KeyPressed(DIK_SPACE) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(3) ) )
		{
			RepairAnimationPlaying = true;
			ChangeAnimation(RepairAnim);
			RepairAnimationTimer = 2.5f;
			AudioManager::GetInstance()->playSFX("machine_fix");
		}
	}

	//Check Point
	if( colliding.GetTag() == "Check Point")
	{
		SetCheckPointData();
	}
	//Gate
	if(colliding.GetObjectID() == "Gate")
	{
		if(other.Side == CollisionSide::BOTTOM)
			positionY += other.Overlap.bottom - other.Overlap.top;
		if(other.Side == CollisionSide::TOP)
			positionY -= other.Overlap.bottom - other.Overlap.top;


		if(other.Side == CollisionSide::LEFT)
			positionX -= other.Overlap.right - other.Overlap.left;
		if(other.Side == CollisionSide::RIGHT)
			positionX += other.Overlap.right - other.Overlap.left;

		//Physics::UpdateColRect( hitBox, positionX, positionY );

		int test = 1000;
	}

	if (colliding.GetTag() == "exit door" && (CSGD_DirectInput::GetInstance()->KeyDown(DIK_SPACE) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(3)))
	{
		isOver = true;
	}


	///////////////////////////////////////////////
	//Moving platform
	if( colliding.GetObjectID() == "MovingPlatform")
	{
		////Get the moving platform
		//GameObject* tempObj = &colliding;
		//MovingPlatform* platform = (MovingPlatform*) tempObj;

		////Get the velocity of the platform
		//tVector2D platformVel = platform->GetVelocity();

		//positionX += platformVel.fX*0.0051f;
		//positionY += platformVel.fY*0.0051f;

		//Don't fall!
		//isGrounded = true;
		//if(other.Overlap.bottom - other.Overlap.top > other.Overlap.right - other.Overlap.left)
		//{
		//	if(other.Side == TOP)
		//	{
		//		if(other.Overlap.bottom - other.Overlap.top > 6)
		//			positionY -= (other.Overlap.bottom - other.Overlap.top);
		//	}
		//}

	}

#pragma region UNUSED CODE
	///////////////////////////////////////////////
	//Conveyor belt
	if( colliding.GetObjectID() == "ConveyorBelt" || colliding.GetTag() == "ConveyorBelt")
	{
		return;
	}
	/*	//Get the moving platform
	GameObject* tempObj = &colliding;
	ConveyorBelt* belt = (ConveyorBelt*) tempObj;

	//if(positionX > colliding.GetPosX() + (colliding.GetHitBox().right-colliding.GetHitBox().left))
	//{
	//	canMoveLeft = false;
	//}
	//else if (positionX < colliding.GetPosX())
	//{
	//	canMoveRight = false;
	//}

	isGrounded = true;
	//if(other.Overlap.bottom - other.Overlap.top > other.Overlap.right - other.Overlap.left)
	//{
	//	if(other.Side == TOP)
	//	{
	//		if(other.Overlap.bottom - other.Overlap.top > 6)
	//			positionY -= (other.Overlap.bottom - other.Overlap.top);
	//	}
	//}
	//horizontal
	if(!belt->getIsVertical())
	{
	if(other.Side == TOP)
	{
	if(belt->getIsClockwise())
	{
	positionX += belt->getSpeed();
	}
	else
	{
	positionX -= belt->getSpeed();
	}

	}
	else if (other.Side == BOTTOM)
	{
	if(belt->getIsClockwise())
	{
	positionX -= belt->getSpeed();
	}
	else
	{
	positionX += belt->getSpeed();
	}
	}
	}
	//vertical
	else
	{
	if(other.Side == LEFT)
	{
	if(belt->getIsClockwise())
	{
	positionY += belt->getSpeed();
	}
	else
	{
	positionY -= belt->getSpeed();
	}

	}
	else if (other.Side == RIGHT)
	{
	if(belt->getIsClockwise())
	{
	positionY -= belt->getSpeed();
	}
	else
	{
	positionY += belt->getSpeed();
	}
	}
	}
	}*/
#pragma endregion

	if( colliding.GetTag() == "pit")
	{
		ResetToCheckPoint();
	}

	if( colliding.GetObjectID() == "Button" )
	{
		//if(other.Side == CollisionSide::BOTTOM)
		//	positionY += other.Overlap.bottom - other.Overlap.top + 1;
		//if(other.Side == CollisionSide::TOP)
		//	positionY -= other.Overlap.bottom - other.Overlap.top - 1;
		//if(other.Side == CollisionSide::LEFT)
		//	positionX -= other.Overlap.right - other.Overlap.left - 1;
		//if(other.Side == CollisionSide::RIGHT)
		//	positionX += other.Overlap.right - other.Overlap.left + 1;

	}

	//Collision with Crumbling Block
	if( colliding.GetObjectID() == "Crumbling Block")
	{
		//Same collision as floor if crumb is active

		if( colliding.GetIsActive() )
		{

			if(other.Side == CollisionSide::BOTTOM)
			{
				positionY += other.Overlap.bottom - other.Overlap.top;
				int test =other.Overlap.bottom - other.Overlap.top;
			}
			else if(other.Side == CollisionSide::TOP)
			{
				positionY -= other.Overlap.bottom - other.Overlap.top;
				int test =other.Overlap.bottom - other.Overlap.top;
			}
			else if(other.Side == CollisionSide::LEFT)
				positionX -= other.Overlap.right - other.Overlap.left;
			else if(other.Side == CollisionSide::RIGHT)
				positionX += other.Overlap.right - other.Overlap.left;
		}
	}


	if(colliding.GetObjectID() == "Metal Box" )
	{
		switch(other.Side)
		{
		case LEFT:
			{
				LONG totalAmtToMove = other.Overlap.right - other.Overlap.left;
				LONG amtToMove = totalAmtToMove / 2;
				LONG amtLeftToMove = amtToMove - dynamic_cast<MetalBox*>(&colliding)->Push(other.Side, amtToMove);
				positionX -= (amtToMove + amtLeftToMove +1);
			}
			break;
		case RIGHT:
			{
				LONG totalAmtToMove = other.Overlap.right - other.Overlap.left;
				LONG amtToMove = totalAmtToMove / 2;
				LONG amtLeftToMove = amtToMove - dynamic_cast<MetalBox*>(&colliding)->Push(other.Side, amtToMove);
				positionX += (amtToMove + amtLeftToMove + 1);
			}
			break;
		case TOP:
			{
				LONG totalAmtToMove = other.Overlap.bottom - other.Overlap.top;
				LONG amtToMove = totalAmtToMove / 2;
				LONG amtLeftToMove = amtToMove - dynamic_cast<MetalBox*>(&colliding)->Push(other.Side, amtToMove);
				positionY -= (amtToMove + amtLeftToMove + 1);
			}
			break;
		case BOTTOM:
			{
				LONG totalAmtToMove = other.Overlap.bottom - other.Overlap.top;
				dynamic_cast<MetalBox*>(&colliding)->Push(other.Side, totalAmtToMove);
			}
			break;
		}
		this->SetHitBox(Physics::UpdateColRect(this->hitBox, (int)this->positionX, (int)this->positionY));
		colliding.SetHitBox(Physics::UpdateColRect(colliding.GetHitBox(), (int)colliding.GetPosX(), (int)colliding.GetPosY()));

	}

	if(colliding.GetObjectID() == "Wood Box" )
	{
		switch(other.Side)
		{
		case LEFT:
			{
				LONG totalAmtToMove = other.Overlap.right - other.Overlap.left;
				LONG amtToMove = totalAmtToMove / 2;
				LONG amtLeftToMove = amtToMove - dynamic_cast<WoodBox*>(&colliding)->Push(other.Side, amtToMove);
				positionX -= (amtToMove + amtLeftToMove +1);
			}
			break;
		case RIGHT:
			{
				LONG totalAmtToMove = other.Overlap.right - other.Overlap.left;
				LONG amtToMove = totalAmtToMove / 2;
				LONG amtLeftToMove = amtToMove - dynamic_cast<WoodBox*>(&colliding)->Push(other.Side, amtToMove);
				positionX += (amtToMove + amtLeftToMove + 1);
			}
			break;
		case TOP:
			{
				LONG totalAmtToMove = other.Overlap.bottom - other.Overlap.top;
				LONG amtToMove = totalAmtToMove / 2;
				LONG amtLeftToMove = amtToMove - dynamic_cast<WoodBox*>(&colliding)->Push(other.Side, amtToMove);
				positionY -= (amtToMove + amtLeftToMove + 1);
			}
			break;
		case BOTTOM:
			{
				LONG totalAmtToMove = other.Overlap.bottom - other.Overlap.top;
				dynamic_cast<WoodBox*>(&colliding)->Push(other.Side, totalAmtToMove);
			}
			break;
		}
		this->SetHitBox(Physics::UpdateColRect(this->hitBox, (int)this->positionX, (int)this->positionY));
		colliding.SetHitBox(Physics::UpdateColRect(colliding.GetHitBox(), (int)colliding.GetPosX(), (int)colliding.GetPosY()));

	}

	if(colliding.GetObjectID() == "Radiation Barrel" )
	{
		switch(other.Side)
		{
		case LEFT:
			{
				LONG totalAmtToMove = other.Overlap.right - other.Overlap.left;
				LONG amtToMove = totalAmtToMove / 2;
				LONG amtLeftToMove = amtToMove - dynamic_cast<RadiationBarrel*>(&colliding)->Push(other.Side, amtToMove);
				positionX -= (amtToMove + amtLeftToMove +1);
			}
			break;
		case RIGHT:
			{
				LONG totalAmtToMove = other.Overlap.right - other.Overlap.left;
				LONG amtToMove = totalAmtToMove / 2;
				LONG amtLeftToMove = amtToMove - dynamic_cast<RadiationBarrel*>(&colliding)->Push(other.Side, amtToMove);
				positionX += (amtToMove + amtLeftToMove + 1);
			}
			break;
		case TOP:
			{
				LONG totalAmtToMove = other.Overlap.bottom - other.Overlap.top;
				LONG amtToMove = totalAmtToMove / 2;
				LONG amtLeftToMove = amtToMove - dynamic_cast<RadiationBarrel*>(&colliding)->Push(other.Side, amtToMove);
				positionY -= (amtToMove + amtLeftToMove + 1);
			}
			break;
		case BOTTOM:
			{
				LONG totalAmtToMove = other.Overlap.bottom - other.Overlap.top;
				dynamic_cast<RadiationBarrel*>(&colliding)->Push(other.Side, totalAmtToMove);
			}
			break;
		}
		this->SetHitBox(Physics::UpdateColRect(this->hitBox, (int)this->positionX, (int)this->positionY));
		colliding.SetHitBox(Physics::UpdateColRect(colliding.GetHitBox(), (int)colliding.GetPosX(), (int)colliding.GetPosY()));

	}


	//Update col rect
	/*RECT CalColRect = ActiveAnimation->GetCollisionBox();


	//Get Width and Height
	int CalColRectWidth = CalColRect.right - CalColRect.left;
	int CalColRectHeight = CalColRect.bottom - CalColRect.top;

	//Calc Cur hit box
	CalColRect.left = (LONG)this->positionX;
	CalColRect.top = (LONG)this->positionY;
	CalColRect.right = CalColRect.left + CalColRectWidth;
	CalColRect.bottom = CalColRect.top + CalColRectHeight;

	////Offset by the Anchor Point
	//POINT tempAnch = IdolAnim.GetCurrAnchorPoint();
	//CalColRect.left -= tempAnch.x - CalColRectWidth / 2;
	//CalColRect.right  -= tempAnch.x - CalColRectWidth / 2;
	//CalColRect.top -= tempAnch.y - CalColRectHeight / 2;
	//CalColRect.bottom -= tempAnch.y - CalColRectHeight / 2;

	POINT tempAnch = IdolAnim.GetCurrAnchorPoint();
	CalColRect.left -= CalColRectWidth / 2;
	CalColRect.right  -=  CalColRectWidth / 2;
	CalColRect.top -=  CalColRectHeight / 2;
	CalColRect.bottom -=  CalColRectHeight / 2;

	int WidthSolid = 85;
	int HeightSolid = 115;
	//Test Solid Col Box
	RECT SolidColRect = { (long)positionX, (long)positionY, (long)positionX + WidthSolid, (long)positionY + HeightSolid };

	SolidColRect.left -= WidthSolid / 2;
	SolidColRect.right  -=  WidthSolid / 2;
	SolidColRect.top -=  HeightSolid / 2;
	SolidColRect.bottom -=  HeightSolid / 2;

	SetHitBox(SolidColRect);*/

}

// Author: Joshua Bramer
// Description:
//		- Tells the player to deactivate the Grapple Arm and the Thermo.
// Parameters:
//		deactivate	-	Whether or not to deactivate the tools.
void Player::DeactivateToolsFromRadiation(bool deactivate)
{
	if(deactivate == true)
		toolManager->Disable();
	else
		toolManager->Enable();
}

void Player::ActivateMovement(void)
{
	areControlsLocked = false;
}

void Player::DeactivateMovement(void)
{
	areControlsLocked = true;
}

void Player::HandleInput(float deltaTime)
{
	//Reset can be done with r in any scheme
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_R) )
	{
		GamePlayState::GetInstance()->ReplayLevel();
	}

	switch(ProfileManager::GetInstance()->getCurProfile().getControlScheme())
	{
		//WASD-JKL
#pragma region WASD-JKL
	case 0:
		{
			if (!areControlsLocked)
			{
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_W))
				{
					heading.fY = 1;
					if(!(frontArm->GetFrozen()))
						frontArm->SetVerticalHeading( LOOKINGUP );
					if(!(RearArm->GetFrozen()))
						RearArm->SetVerticalHeading( LOOKINGUP );
				}
				else if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_S))
				{
					heading.fY = -1;
					if(!(frontArm->GetFrozen()))
						frontArm->SetVerticalHeading( LOOKINGDOWN );
					if(!(RearArm->GetFrozen()))
						RearArm->SetVerticalHeading( LOOKINGDOWN );
				}
				else
				{
					heading.fY = 0;
					if(!(frontArm->GetFrozen()))
						frontArm->SetVerticalHeading( LOOKINGSTRAIGHT );
					if(!(RearArm->GetFrozen()))
						RearArm->SetVerticalHeading( LOOKINGSTRAIGHT );
				}

				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_A))
				{
					if(canMoveLeft)
						velX = -210.0f;
					else
						velX = 0;
					heading.fX = 1;
					frontArm->SetFacing( FACINGLEFT );
					RearArm->SetFacing( FACINGLEFT );

					AnimationFacingLeft = true;
				}
				else if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_D))
				{
					if(canMoveRight)
						velX = 210.0f;
					else
						velX = 0;
					heading.fX = -1;
					frontArm->SetFacing( FACINGRIGHT );
					RearArm->SetFacing( FACINGRIGHT );

					AnimationFacingLeft = false;
				}
				else
				{
					velX = 0.0f;
				}
			}
			//else // controsl are locked - only change heading
			//{
			//	if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_W))
			//	{
			//		heading.fY = 1;
			//		if(!(frontArm->GetFrozen()))
			//			frontArm->SetVerticalHeading( LOOKINGUP );
			//		if(!(RearArm->GetFrozen()))
			//			RearArm->SetVerticalHeading( LOOKINGUP );
			//	}
			//	else if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_S))
			//	{
			//		heading.fY = -1;
			//		if(!(frontArm->GetFrozen()))
			//			frontArm->SetVerticalHeading( LOOKINGDOWN );
			//		if(!(RearArm->GetFrozen()))
			//			RearArm->SetVerticalHeading( LOOKINGDOWN );
			//	}
			//	else
			//	{
			//		heading.fY = 0;
			//		if(!(frontArm->GetFrozen()))
			//			frontArm->SetVerticalHeading( LOOKINGSTRAIGHT );
			//		if(!(RearArm->GetFrozen()))
			//			RearArm->SetVerticalHeading( LOOKINGSTRAIGHT );
			//	}

			//}

			//Change the animation orientation depending on the direction of movement
			if( RepairAnimationPlaying == false && CSGD_DirectInput::GetInstance()->KeyPressed(DIK_A) )
			{
				if( ActiveAnimation != &RepairAnim && ActiveAnimation != &DeathAnim && ActiveAnimation != &RunningAnim) 
				{
					this->ChangeAnimation(this->RunningAnim);
					//this->ActiveAnimation->FlipTheImage(true);
				}

			}
			if( RepairAnimationPlaying == false && CSGD_DirectInput::GetInstance()->KeyPressed(DIK_D) )
			{
				if( ActiveAnimation != &RepairAnim && ActiveAnimation != &DeathAnim  ) 
				{
					this->ChangeAnimation(this->RunningAnim);
					//this->ActiveAnimation->FlipTheImage(false);
				}

			}
			if( ActiveAnimation != &IdolAnim && !CSGD_DirectInput::GetInstance()->KeyDown(DIK_D) && !CSGD_DirectInput::GetInstance()->KeyDown(DIK_A))
			{
				if( ActiveAnimation != &RepairAnim && ActiveAnimation != &DeathAnim  ) 
					this->ChangeAnimation(this->IdolAnim);
			}


			if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_J))
			{
				toolManager->ToggleTool(GRAPPLE);
				hud->SetHudTimer(3.1f);
			}
			if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_K))
			{
				toolManager->ToggleTool(MAGNET);
				hud->SetHudTimer(3.1f);
			}
			if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_L))
			{
				toolManager->ToggleTool(THERMO);
				hud->SetHudTimer(3.1f);
			}
			if(CSGD_DirectInput::GetInstance()->KeyReleased(DIK_L))
			{
				toolManager->ToggleTool(THERMO);
				hud->SetHudTimer(3.1f);
			}
			if(!CSGD_DirectInput::GetInstance()->KeyDown(DIK_L))
			{
				if(toolManager->GetIsThermo())
					toolManager->ToggleTool(THERMO);
			}

			if(CSGD_DirectInput::GetInstance()->KeyReleased(DIK_I))
			{
				toolManager->Disable();
				//hud->SetHudTimer(3.1f);
			}
			if(CSGD_DirectInput::GetInstance()->KeyReleased(DIK_O))
			{
				toolManager->Enable();
				//hud->SetHudTimer(3.1f);
			} 

			if(areControlsLocked)
			{
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_W) && canMoveUp)
				{
					if(grappleddir == Up)
					{
						toolManager->GetGrap().SetDifY( toolManager->GetGrap().GetDifY() - 300 * deltaTime);
						if( toolManager->GetGrap().GetDifY() < 70 )
							toolManager->GetGrap().SetDifY( 70 );
					}
					else if(grappleddir == Down)
					{
						float newdif = toolManager->GetGrap().GetDifY();
						newdif -= 300 * deltaTime;

						toolManager->GetGrap().SetDifY( newdif );
						if( toolManager->GetGrap().GetDifY() < -256 )
							toolManager->GetGrap().SetDifY( -256 );
					}
				}
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_S) && canMoveDown)
				{
					if(grappleddir == Down)
					{
						toolManager->GetGrap().SetDifY( toolManager->GetGrap().GetDifY() + 300 * deltaTime );
						if( toolManager->GetGrap().GetDifY() > -70 )
							toolManager->GetGrap().SetDifY( -70 );
					}
					else if(grappleddir == Up)
					{
						toolManager->GetGrap().SetDifY( toolManager->GetGrap().GetDifY() + 300 * deltaTime );
						if( toolManager->GetGrap().GetDifY() > 256 )
							toolManager->GetGrap().SetDifY( 256 );
					}
				}
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_A) && canMoveLeft)
				{
					if(grappleddir == Left)
					{
						toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() - 300 * deltaTime );
						if( toolManager->GetGrap().GetDifX() < 70 )
							toolManager->GetGrap().SetDifX( 70 );
					}
					else if(grappleddir == Right)
					{
						toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() - 300 * deltaTime );
						if( toolManager->GetGrap().GetDifX() < -256 )
							toolManager->GetGrap().SetDifX( -256 );
					}
				}
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_D) && canMoveRight)
				{
					if(grappleddir == Left)
					{
						toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() + 300 * deltaTime );
						if( toolManager->GetGrap().GetDifX() > 256 )
							toolManager->GetGrap().SetDifX( 256 );
					}
					else if(grappleddir == Right)
					{
						toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() + 300 * deltaTime );
						if( toolManager->GetGrap().GetDifX() > -70 )
							toolManager->GetGrap().SetDifX( -70 );
					}
				}
			}


			break;
		}
#pragma endregion wasd, jkl
		//Arrow keys, ZXC
#pragma region Arrow Keys, ZXC
	case 1:
		{
			if (!areControlsLocked)
			{
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_UP))
				{
					heading.fY = 1;
					if(!(frontArm->GetFrozen()))
						frontArm->SetVerticalHeading( LOOKINGUP );
					if(!(RearArm->GetFrozen()))
						RearArm->SetVerticalHeading( LOOKINGUP );
				}
				else if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_DOWN))
				{
					heading.fY = -1;
					if(!(frontArm->GetFrozen()))
						frontArm->SetVerticalHeading( LOOKINGDOWN );
					if(!(RearArm->GetFrozen()))
						RearArm->SetVerticalHeading( LOOKINGDOWN );
				}
				else
				{
					heading.fY = 0;
					if(!(frontArm->GetFrozen()))
						frontArm->SetVerticalHeading( LOOKINGSTRAIGHT );
					if(!(RearArm->GetFrozen()))
						RearArm->SetVerticalHeading( LOOKINGSTRAIGHT );
				}

				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_LEFT))
				{
					if(canMoveLeft)
						velX = -210.0f;
					else
						velX = 0;
					heading.fX = 1;
					frontArm->SetFacing( FACINGLEFT );
					RearArm->SetFacing( FACINGLEFT );

					AnimationFacingLeft = true;
				}
				else if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_RIGHT))
				{
					if(canMoveRight)
						velX = 210.0f;
					else
						velX = 0;
					heading.fX = -1;
					frontArm->SetFacing( FACINGRIGHT );
					RearArm->SetFacing( FACINGRIGHT );

					AnimationFacingLeft = false;
				}
				else
				{
					velX = 0.0f;
				}
			}

			//Change the animation orientation depending on the direction of movement
			if( RepairAnimationPlaying == false && CSGD_DirectInput::GetInstance()->KeyPressed(DIK_LEFT) )
			{
				if( ActiveAnimation != &RepairAnim && ActiveAnimation != &DeathAnim && ActiveAnimation != &RunningAnim) 
				{
					this->ChangeAnimation(this->RunningAnim);
					//this->ActiveAnimation->FlipTheImage(true);
				}

			}
			if( RepairAnimationPlaying == false && CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RIGHT) )
			{
				if( ActiveAnimation != &RepairAnim && ActiveAnimation != &DeathAnim  ) 
				{
					this->ChangeAnimation(this->RunningAnim);
					//this->ActiveAnimation->FlipTheImage(false);
				}

			}
			if( ActiveAnimation != &IdolAnim && !CSGD_DirectInput::GetInstance()->KeyDown(DIK_RIGHT) && !CSGD_DirectInput::GetInstance()->KeyDown(DIK_LEFT))
			{
				if( ActiveAnimation != &RepairAnim && ActiveAnimation != &DeathAnim  ) 
					this->ChangeAnimation(this->IdolAnim);
			}


			if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_Z))
			{
				toolManager->ToggleTool(GRAPPLE);
			}
			if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_X))
			{
				toolManager->ToggleTool(MAGNET);
			}
			if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_C))
			{
				toolManager->ToggleTool(THERMO);
			}
			if(CSGD_DirectInput::GetInstance()->KeyReleased(DIK_C))
			{
				toolManager->ToggleTool(THERMO);
			}
			if(!CSGD_DirectInput::GetInstance()->KeyDown(DIK_C))
			{
				if(toolManager->GetIsThermo())
					toolManager->ToggleTool(THERMO);
			}
			if(CSGD_DirectInput::GetInstance()->KeyReleased(DIK_I))
			{
				toolManager->Disable();
			}
			if(CSGD_DirectInput::GetInstance()->KeyReleased(DIK_O))
			{
				toolManager->Enable();
			} 

			if(areControlsLocked)
			{
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_UP) && canMoveUp)
				{
					if(grappleddir == Up)
					{
						toolManager->GetGrap().SetDifY( toolManager->GetGrap().GetDifY() - 300 * deltaTime);
						if( toolManager->GetGrap().GetDifY() < 70 )
							toolManager->GetGrap().SetDifY( 70 );
					}
					else if(grappleddir == Down)
					{
						float newdif = toolManager->GetGrap().GetDifY();
						newdif -= 300 * deltaTime;

						toolManager->GetGrap().SetDifY( newdif );
						if( toolManager->GetGrap().GetDifY() < -256 )
							toolManager->GetGrap().SetDifY( -256 );
					}
				}
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_DOWN) && canMoveDown)
				{
					if(grappleddir == Down)
					{
						toolManager->GetGrap().SetDifY( toolManager->GetGrap().GetDifY() + 300 * deltaTime );
						if( toolManager->GetGrap().GetDifY() > -70 )
							toolManager->GetGrap().SetDifY( -70 );
					}
					else if(grappleddir == Up)
					{
						toolManager->GetGrap().SetDifY( toolManager->GetGrap().GetDifY() + 300 * deltaTime );
						if( toolManager->GetGrap().GetDifY() > 256 )
							toolManager->GetGrap().SetDifY( 256 );
					}
				}
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_LEFT) && canMoveLeft)
				{
					if(grappleddir == Left)
					{
						toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() - 300 * deltaTime );
						if( toolManager->GetGrap().GetDifX() < 70 )
							toolManager->GetGrap().SetDifX( 70 );
					}
					else if(grappleddir == Right)
					{
						toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() - 300 * deltaTime );
						if( toolManager->GetGrap().GetDifX() < -256 )
							toolManager->GetGrap().SetDifX( -256 );
					}
				}
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_RIGHT) && canMoveRight)
				{
					if(grappleddir == Left)
					{
						toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() + 300 * deltaTime );
						if( toolManager->GetGrap().GetDifX() > 256 )
							toolManager->GetGrap().SetDifX( 256 );
					}
					else if(grappleddir == Right)
					{
						toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() + 300 * deltaTime );
						if( toolManager->GetGrap().GetDifX() > -70 )
							toolManager->GetGrap().SetDifX( -70 );
					}
				}
			}
			break;
		}
#pragma endregion Arrow keys, ZXC
		//WASD, ZXC
#pragma region WASD, ZXC
	case 2:
		{
			if (!areControlsLocked)
			{
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_W))
				{
					heading.fY = 1;
					if(!(frontArm->GetFrozen()))
						frontArm->SetVerticalHeading( LOOKINGUP );
					if(!(RearArm->GetFrozen()))
						RearArm->SetVerticalHeading( LOOKINGUP );
				}
				else if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_S))
				{
					heading.fY = -1;
					if(!(frontArm->GetFrozen()))
						frontArm->SetVerticalHeading( LOOKINGDOWN );
					if(!(RearArm->GetFrozen()))
						RearArm->SetVerticalHeading( LOOKINGDOWN );
				}
				else
				{
					heading.fY = 0;
					if(!(frontArm->GetFrozen()))
						frontArm->SetVerticalHeading( LOOKINGSTRAIGHT );
					if(!(RearArm->GetFrozen()))
						RearArm->SetVerticalHeading( LOOKINGSTRAIGHT );
				}

				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_A))
				{
					if(canMoveLeft)
						velX = -210.0f;
					else
						velX = 0;
					heading.fX = 1;
					frontArm->SetFacing( FACINGLEFT );
					RearArm->SetFacing( FACINGLEFT );

					AnimationFacingLeft = true;
				}
				else if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_D))
				{
					if(canMoveRight)
						velX = 210.0f;
					else
						velX = 0;
					heading.fX = -1;
					frontArm->SetFacing( FACINGRIGHT );
					RearArm->SetFacing( FACINGRIGHT );

					AnimationFacingLeft = false;
				}
				else
				{
					velX = 0.0f;
				}
			}

			//Change the animation orientation depending on the direction of movement
			if( CSGD_DirectInput::GetInstance()->KeyPressed(DIK_A) )
			{
				if( ActiveAnimation != &RepairAnim && ActiveAnimation != &DeathAnim  ) 
				{
					this->ChangeAnimation(this->RunningAnim);
					this->ActiveAnimation->FlipTheImage(true);
				}
			}
			if( CSGD_DirectInput::GetInstance()->KeyPressed(DIK_D) )
			{
				if( ActiveAnimation != &RepairAnim && ActiveAnimation != &DeathAnim  ) 
				{
					this->ChangeAnimation(this->RunningAnim);
					this->ActiveAnimation->FlipTheImage(false);
				}
			}
			if( ActiveAnimation != &IdolAnim && !CSGD_DirectInput::GetInstance()->KeyDown(DIK_D) && !CSGD_DirectInput::GetInstance()->KeyDown(DIK_A))
			{
				if ( ActiveAnimation != &RepairAnim )
					this->ChangeAnimation(this->IdolAnim);
			}


			if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_Z))
			{
				toolManager->ToggleTool(GRAPPLE);
			}
			if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_X))
			{
				toolManager->ToggleTool(MAGNET);
			}
			if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_C))
			{
				toolManager->ToggleTool(THERMO);
			}
			if(CSGD_DirectInput::GetInstance()->KeyReleased(DIK_C))
			{
				toolManager->ToggleTool(THERMO);
			}
			if(!CSGD_DirectInput::GetInstance()->KeyDown(DIK_C))
			{
				if(toolManager->GetIsThermo())
					toolManager->ToggleTool(THERMO);
			}
			if(CSGD_DirectInput::GetInstance()->KeyReleased(DIK_I))
			{
				toolManager->Disable();
			}
			if(CSGD_DirectInput::GetInstance()->KeyReleased(DIK_O))
			{
				toolManager->Enable();
			} 

			if(areControlsLocked)
			{
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_W) && canMoveUp)
				{
					if(grappleddir == Up)
					{
						toolManager->GetGrap().SetDifY( toolManager->GetGrap().GetDifY() - 300 * deltaTime);
						if( toolManager->GetGrap().GetDifY() < 70 )
							toolManager->GetGrap().SetDifY( 70 );
					}
					else if(grappleddir == Down)
					{
						float newdif = toolManager->GetGrap().GetDifY();
						newdif -= 300 * deltaTime;

						toolManager->GetGrap().SetDifY( newdif );
						if( toolManager->GetGrap().GetDifY() < -256 )
							toolManager->GetGrap().SetDifY( -256 );
					}
				}
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_S) && canMoveDown)
				{
					if(grappleddir == Down)
					{
						toolManager->GetGrap().SetDifY( toolManager->GetGrap().GetDifY() + 300 * deltaTime );
						if( toolManager->GetGrap().GetDifY() > -70 )
							toolManager->GetGrap().SetDifY( -70 );
					}
					else if(grappleddir == Up)
					{
						toolManager->GetGrap().SetDifY( toolManager->GetGrap().GetDifY() + 300 * deltaTime );
						if( toolManager->GetGrap().GetDifY() > 256 )
							toolManager->GetGrap().SetDifY( 256 );
					}
				}
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_A) && canMoveLeft)
				{
					if(grappleddir == Left)
					{
						toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() - 300 * deltaTime );
						if( toolManager->GetGrap().GetDifX() < 70 )
							toolManager->GetGrap().SetDifX( 70 );
					}
					else if(grappleddir == Right)
					{
						toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() - 300 * deltaTime );
						if( toolManager->GetGrap().GetDifX() < -256 )
							toolManager->GetGrap().SetDifX( -256 );
					}
				}
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_D) && canMoveRight)
				{
					if(grappleddir == Left)
					{
						toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() + 300 * deltaTime );
						if( toolManager->GetGrap().GetDifX() > 256 )
							toolManager->GetGrap().SetDifX( 256 );
					}
					else if(grappleddir == Right)
					{
						toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() + 300 * deltaTime );
						if( toolManager->GetGrap().GetDifX() > -70 )
							toolManager->GetGrap().SetDifX( -70 );
					}
				}
			}
			break;
		}
#pragma endregion WASD, ZXC
		//Arrow keys, JKL
#pragma region Arrow keys, JKL
	case 3:
		{
			if (!areControlsLocked)
			{
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_UP))
				{
					heading.fY = 1;
					if(!(frontArm->GetFrozen()))
						frontArm->SetVerticalHeading( LOOKINGUP );
					if(!(RearArm->GetFrozen()))
						RearArm->SetVerticalHeading( LOOKINGUP );
				}
				else if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_DOWN))
				{
					heading.fY = -1;
					if(!(frontArm->GetFrozen()))
						frontArm->SetVerticalHeading( LOOKINGDOWN );
					if(!(RearArm->GetFrozen()))
						RearArm->SetVerticalHeading( LOOKINGDOWN );
				}
				else
				{
					heading.fY = 0;
					if(!(frontArm->GetFrozen()))
						frontArm->SetVerticalHeading( LOOKINGSTRAIGHT );
					if(!(RearArm->GetFrozen()))
						RearArm->SetVerticalHeading( LOOKINGSTRAIGHT );
				}

				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_LEFT))
				{
					if(canMoveLeft)
						velX = -210.0f;
					else
						velX = 0;
					heading.fX = 1;
					frontArm->SetFacing( FACINGLEFT );
					RearArm->SetFacing( FACINGLEFT );

					AnimationFacingLeft = true;
				}
				else if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_RIGHT))
				{
					if(canMoveRight)
						velX = 210.0f;
					else
						velX = 0;
					heading.fX = -1;
					frontArm->SetFacing( FACINGRIGHT );
					RearArm->SetFacing( FACINGRIGHT );

					AnimationFacingLeft = false;
				}
				else
				{
					velX = 0.0f;
				}
			}

			//Change the animation orientation depending on the direction of movement
			if( CSGD_DirectInput::GetInstance()->KeyPressed(DIK_LEFT) )
			{
				if( ActiveAnimation != &RepairAnim && ActiveAnimation != &DeathAnim  ) 
				{
					this->ChangeAnimation(this->RunningAnim);
					this->ActiveAnimation->FlipTheImage(true);
				}
			}
			if( CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RIGHT) )
			{
				if( ActiveAnimation != &RepairAnim && ActiveAnimation != &DeathAnim  ) 
				{
					this->ChangeAnimation(this->RunningAnim);
					this->ActiveAnimation->FlipTheImage(false);
				}
			}
			if( ActiveAnimation != &IdolAnim && !CSGD_DirectInput::GetInstance()->KeyDown(DIK_RIGHT) && !CSGD_DirectInput::GetInstance()->KeyDown(DIK_LEFT))
			{
				if( ActiveAnimation != &RepairAnim && ActiveAnimation != &DeathAnim  ) 
					this->ChangeAnimation(this->IdolAnim);
			}


			if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_J))
			{
				toolManager->ToggleTool(GRAPPLE);
			}
			if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_K))
			{
				toolManager->ToggleTool(MAGNET);
			}
			if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_L))
			{
				toolManager->ToggleTool(THERMO);
			}
			if(CSGD_DirectInput::GetInstance()->KeyReleased(DIK_L))
			{
				toolManager->ToggleTool(THERMO);
			}
			if(!CSGD_DirectInput::GetInstance()->KeyDown(DIK_L))
			{
				if(toolManager->GetIsThermo())
					toolManager->ToggleTool(THERMO);
			}
			if(CSGD_DirectInput::GetInstance()->KeyReleased(DIK_I))
			{
				toolManager->Disable();
			}
			if(CSGD_DirectInput::GetInstance()->KeyReleased(DIK_O))
			{
				toolManager->Enable();
			} 

			if(areControlsLocked)
			{
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_UP) && canMoveUp)
				{
					if(grappleddir == Up)
					{
						toolManager->GetGrap().SetDifY( toolManager->GetGrap().GetDifY() - 300 * deltaTime);
						if( toolManager->GetGrap().GetDifY() < 70 )
							toolManager->GetGrap().SetDifY( 70 );
					}
					else if(grappleddir == Down)
					{
						float newdif = toolManager->GetGrap().GetDifY();
						newdif -= 300 * deltaTime;

						toolManager->GetGrap().SetDifY( newdif );
						if( toolManager->GetGrap().GetDifY() < -256 )
							toolManager->GetGrap().SetDifY( -256 );
					}
				}
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_DOWN) && canMoveDown)
				{
					if(grappleddir == Down)
					{
						toolManager->GetGrap().SetDifY( toolManager->GetGrap().GetDifY() + 300 * deltaTime );
						if( toolManager->GetGrap().GetDifY() > -70 )
							toolManager->GetGrap().SetDifY( -70 );
					}
					else if(grappleddir == Up)
					{
						toolManager->GetGrap().SetDifY( toolManager->GetGrap().GetDifY() + 300 * deltaTime );
						if( toolManager->GetGrap().GetDifY() > 256 )
							toolManager->GetGrap().SetDifY( 256 );
					}
				}
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_LEFT) && canMoveLeft)
				{
					if(grappleddir == Left)
					{
						toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() - 300 * deltaTime );
						if( toolManager->GetGrap().GetDifX() < 70 )
							toolManager->GetGrap().SetDifX( 70 );
					}
					else if(grappleddir == Right)
					{
						toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() - 300 * deltaTime );
						if( toolManager->GetGrap().GetDifX() < -256 )
							toolManager->GetGrap().SetDifX( -256 );
					}
				}
				if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_RIGHT) && canMoveRight)
				{
					if(grappleddir == Left)
					{
						toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() + 300 * deltaTime );
						if( toolManager->GetGrap().GetDifX() > 256 )
							toolManager->GetGrap().SetDifX( 256 );
					}
					else if(grappleddir == Right)
					{
						toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() + 300 * deltaTime );
						if( toolManager->GetGrap().GetDifX() > -70 )
							toolManager->GetGrap().SetDifX( -70 );
					}
				}
			}
			break;
#pragma endregion Arrow keys, JKL
			//XBOX controller
#pragma region XBOX Controller
	case 4:
		if (!areControlsLocked)
		{
			//Reset
			if(CSGD_DirectInput::GetInstance()->JoystickButtonPressed(6))
			{
				ResetToCheckPoint();
			}

			if(CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() < 0 || CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_UP))
			{
				heading.fY = 1;
				if(!(frontArm->GetFrozen()))
					frontArm->SetVerticalHeading( LOOKINGUP );
				if(!(RearArm->GetFrozen()))
					RearArm->SetVerticalHeading( LOOKINGUP );
			}
			else if(CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() > 0 || CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_DOWN))
			{
				heading.fY = -1;
				if(!(frontArm->GetFrozen()))
					frontArm->SetVerticalHeading( LOOKINGDOWN );
				if(!(RearArm->GetFrozen()))
					RearArm->SetVerticalHeading( LOOKINGDOWN );
			}
			else
			{
				heading.fY = 0;
				if(!(frontArm->GetFrozen()))
					frontArm->SetVerticalHeading( LOOKINGSTRAIGHT );
				if(!(RearArm->GetFrozen()))
					RearArm->SetVerticalHeading( LOOKINGSTRAIGHT );
			}

			if(CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized() < 0 || CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_LEFT))
			{
				if(canMoveLeft)
					velX = -210.0f;
				else
					velX = 0;
				heading.fX = 1;
				frontArm->SetFacing( FACINGLEFT );
				RearArm->SetFacing( FACINGLEFT );

				AnimationFacingLeft = true;
			}
			else if(CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized() > 0 || CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_RIGHT))
			{
				if(canMoveRight)
					velX = 210.0f;
				else
					velX = 0;
				heading.fX = -1;
				frontArm->SetFacing( FACINGRIGHT );
				RearArm->SetFacing( FACINGRIGHT );

				AnimationFacingLeft = false;
			}
			else
			{
				velX = 0.0f;
			}
		}
		if( CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized() < 0 || CSGD_DirectInput::GetInstance()->JoystickDPadPressed(DIR_LEFT) )
		{
			if( ActiveAnimation != &RepairAnim && ActiveAnimation != &DeathAnim && ActiveAnimation != &RunningAnim ) 
			{
				this->ChangeAnimation(this->RunningAnim);
				this->ActiveAnimation->FlipTheImage(true);
			}
		}
		if( CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized() > 0 || CSGD_DirectInput::GetInstance()->JoystickDPadPressed(DIR_RIGHT) )
		{
			if( ActiveAnimation != &RepairAnim && ActiveAnimation != &DeathAnim && ActiveAnimation != &RunningAnim ) 
			{
				this->ChangeAnimation(this->RunningAnim);
				this->ActiveAnimation->FlipTheImage(false);
			}
		}
		if( FloatEquals(CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized(),0) && !CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_LEFT)
			&& FloatEquals(CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized(), 0) && !CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_RIGHT))
		{
			this->ChangeAnimation(this->IdolAnim);
		}
		//TODO: Update these buttons!
		if(CSGD_DirectInput::GetInstance()->JoystickButtonPressed(0))
		{
			toolManager->ToggleTool(GRAPPLE);
		}
		if(CSGD_DirectInput::GetInstance()->JoystickButtonPressed(2))
		{
			toolManager->ToggleTool(MAGNET);
		}
		if(CSGD_DirectInput::GetInstance()->JoystickButtonPressed(1))
		{
			toolManager->ToggleTool(THERMO);
		}
		if(CSGD_DirectInput::GetInstance()->JoystickButtonReleased(1))
		{
			toolManager->ToggleTool(THERMO);
		}
		if(!CSGD_DirectInput::GetInstance()->JoystickButtonDown(1))
		{
			if(toolManager->GetIsThermo())
				toolManager->ToggleTool(THERMO);
		}

		if(areControlsLocked)
		{
			if(CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() < 0 || CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_UP) && canMoveUp)
			{
				if(grappleddir == Up)
				{
					toolManager->GetGrap().SetDifY( toolManager->GetGrap().GetDifY() - 300 * deltaTime);
					if( toolManager->GetGrap().GetDifY() < 70 )
						toolManager->GetGrap().SetDifY( 70 );
				}
				else if(grappleddir == Down)
				{
					float newdif = toolManager->GetGrap().GetDifY();
					newdif -= 300 * deltaTime;

					toolManager->GetGrap().SetDifY( newdif );
					if( toolManager->GetGrap().GetDifY() < -256 )
						toolManager->GetGrap().SetDifY( -256 );
				}
			}
			if(CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() > 0 || CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_DOWN) && canMoveDown)
			{
				if(grappleddir == Down)
				{
					toolManager->GetGrap().SetDifY( toolManager->GetGrap().GetDifY() + 300 * deltaTime );
					if( toolManager->GetGrap().GetDifY() > -70 )
						toolManager->GetGrap().SetDifY( -70 );
				}
				else if(grappleddir == Up)
				{
					toolManager->GetGrap().SetDifY( toolManager->GetGrap().GetDifY() + 300 * deltaTime );
					if( toolManager->GetGrap().GetDifY() > 256 )
						toolManager->GetGrap().SetDifY( 256 );
				}
			}
			if(CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized() < 0 || CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_LEFT) && canMoveLeft)
			{
				if(grappleddir == Left)
				{
					toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() - 300 * deltaTime );
					if( toolManager->GetGrap().GetDifX() < 70 )
						toolManager->GetGrap().SetDifX( 70 );
				}
				else if(grappleddir == Right)
				{
					toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() - 300 * deltaTime );
					if( toolManager->GetGrap().GetDifX() < -256 )
						toolManager->GetGrap().SetDifX( -256 );
				}
			}
			if(CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized() > 0 || CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_RIGHT) && canMoveRight)
			{
				if(grappleddir == Left)
				{
					toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() + 300 * deltaTime );
					if( toolManager->GetGrap().GetDifX() > 256 )
						toolManager->GetGrap().SetDifX( 256 );
				}
				else if(grappleddir == Right)
				{
					toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() + 300 * deltaTime );
					if( toolManager->GetGrap().GetDifX() > -70 )
						toolManager->GetGrap().SetDifX( -70 );
				}
			}

			//Reset
			if(CSGD_DirectInput::GetInstance()->JoystickButtonPressed(6))
			{
				ResetToCheckPoint();
			}
		}
		break;
#pragma endregion XBOX
		//Arcade machine
#pragma region ARCADE
	case 5:
		//Reset
		if(CSGD_DirectInput::GetInstance()->MouseButtonPressed(1) || CSGD_DirectInput::GetInstance()->MouseButtonPressed(0))
		{
			ResetToCheckPoint();
		}
		if (!areControlsLocked)
		{
			if(CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() < 0 || CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_UP))
			{
				heading.fY = 1;
				if(!(frontArm->GetFrozen()))
					frontArm->SetVerticalHeading( LOOKINGUP );
				if(!(RearArm->GetFrozen()))
					RearArm->SetVerticalHeading( LOOKINGUP );
			}
			else if(CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() > 0 || CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_DOWN))
			{
				heading.fY = -1;
				if(!(frontArm->GetFrozen()))
					frontArm->SetVerticalHeading( LOOKINGDOWN );
				if(!(RearArm->GetFrozen()))
					RearArm->SetVerticalHeading( LOOKINGDOWN );
			}
			else
			{
				heading.fY = 0;
				if(!(frontArm->GetFrozen()))
					frontArm->SetVerticalHeading( LOOKINGSTRAIGHT );
				if(!(RearArm->GetFrozen()))
					RearArm->SetVerticalHeading( LOOKINGSTRAIGHT );
			}

			if(CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized() < 0 || CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_LEFT))
			{
				if(canMoveLeft)
					velX = -210.0f;
				else
					velX = 0;
				heading.fX = 1;
				frontArm->SetFacing( FACINGLEFT );
				RearArm->SetFacing( FACINGLEFT );

				AnimationFacingLeft = true;
			}
			else if(CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized() > 0 || CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_RIGHT))
			{
				if(canMoveRight)
					velX = 210.0f;
				else
					velX = 0;
				heading.fX = -1;
				frontArm->SetFacing( FACINGRIGHT );
				RearArm->SetFacing( FACINGRIGHT );

				AnimationFacingLeft = false;
			}
			else
			{
				velX = 0.0f;
			}
		}
		if( CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized() < 0 || CSGD_DirectInput::GetInstance()->JoystickDPadPressed(DIR_LEFT) )
		{
			if(this->ActiveAnimation != &this->RunningAnim)
			{
				this->ChangeAnimation(this->RunningAnim);
				this->ActiveAnimation->FlipTheImage(true);
			}
		}
		if( CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized() > 0 || CSGD_DirectInput::GetInstance()->JoystickDPadPressed(DIR_RIGHT) )
		{
			if(this->ActiveAnimation != &this->RunningAnim)
			{
				this->ChangeAnimation(this->RunningAnim);
				this->ActiveAnimation->FlipTheImage(false);
			}
		}
		if( FloatEquals(CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized(),0) && !CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_LEFT)
			&& FloatEquals(CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized(), 0) && !CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_RIGHT))
		{
			this->ChangeAnimation(this->IdolAnim);
		}


		//TODO: Update these buttons!
		if(CSGD_DirectInput::GetInstance()->JoystickButtonPressed(0))
		{
			toolManager->ToggleTool(GRAPPLE);
		}
		if(CSGD_DirectInput::GetInstance()->JoystickButtonPressed(1))
		{
			toolManager->ToggleTool(MAGNET);
		}
		if(CSGD_DirectInput::GetInstance()->JoystickButtonPressed(2))
		{
			toolManager->ToggleTool(THERMO);
		}
		//if(CSGD_DirectInput::GetInstance()->JoystickButtonPressed(2))
		//{
		//	toolManager->ToggleTool(THERMO);
		//}

		if(areControlsLocked)
		{
			if(CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() < 0 || CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_UP) && canMoveUp)
			{
				if(grappleddir == Up)
				{
					toolManager->GetGrap().SetDifY( toolManager->GetGrap().GetDifY() - 300 * deltaTime);
					if( toolManager->GetGrap().GetDifY() < 70 )
						toolManager->GetGrap().SetDifY( 70 );
				}
				else if(grappleddir == Down)
				{
					float newdif = toolManager->GetGrap().GetDifY();
					newdif -= 300 * deltaTime;

					toolManager->GetGrap().SetDifY( newdif );
					if( toolManager->GetGrap().GetDifY() < -256 )
						toolManager->GetGrap().SetDifY( -256 );
				}
			}
			if(CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() > 0 || CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_DOWN) && canMoveDown)
			{
				if(grappleddir == Down)
				{
					toolManager->GetGrap().SetDifY( toolManager->GetGrap().GetDifY() + 300 * deltaTime );
					if( toolManager->GetGrap().GetDifY() > -70 )
						toolManager->GetGrap().SetDifY( -70 );
				}
				else if(grappleddir == Up)
				{
					toolManager->GetGrap().SetDifY( toolManager->GetGrap().GetDifY() + 300 * deltaTime );
					if( toolManager->GetGrap().GetDifY() > 256 )
						toolManager->GetGrap().SetDifY( 256 );
				}
			}
			if(CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized() < 0 || CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_LEFT) && canMoveLeft)
			{
				if(grappleddir == Left)
				{
					toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() - 300 * deltaTime );
					if( toolManager->GetGrap().GetDifX() < 70 )
						toolManager->GetGrap().SetDifX( 70 );
				}
				else if(grappleddir == Right)
				{
					toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() - 300 * deltaTime );
					if( toolManager->GetGrap().GetDifX() < -256 )
						toolManager->GetGrap().SetDifX( -256 );
				}
			}
			if(CSGD_DirectInput::GetInstance()->JoystickGetLStickXNormalized() > 0 || CSGD_DirectInput::GetInstance()->JoystickDPadDown(DIR_RIGHT) && canMoveRight)
			{
				if(grappleddir == Left)
				{
					toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() + 300 * deltaTime );
					if( toolManager->GetGrap().GetDifX() > 256 )
						toolManager->GetGrap().SetDifX( 256 );
				}
				else if(grappleddir == Right)
				{
					toolManager->GetGrap().SetDifX( toolManager->GetGrap().GetDifX() + 300 * deltaTime );
					if( toolManager->GetGrap().GetDifX() > -70 )
						toolManager->GetGrap().SetDifX( -70 );
				}
			}
		}
		break;
#pragma endregion ARCADE
		}
	}
}

void Player::HandleEvent(Event* pEvent)	
{
	if( Immune == false )
	{
		if( pEvent->GetEventID() == "KillPlayer")
		{
			if( DeathAnimationPlaying == false )
			{
				//Get the timer for the animation
				DeathAnimationTimer = DeathAnim.CurrentAnimationLoopTime();
				ImmuneTimer = DeathAnimationTimer + 0.5f;

				//Set the animation to death anim
				ChangeAnimation(DeathAnim);

				//Lock Player movement
				DeactivateMovement();

				//Prevent from looping and resetting time! 
				DeathAnimationPlaying = true; 
				Immune = true;
			}
		}
	}
}

void Player::ChangeAnimation(Animation &switchto )
{
	if( ActiveAnimation != nullptr)
	{
		ActiveAnimation->SetPause(true);
		ActiveAnimation->RestartAnimation();
	}
	ActiveAnimation = &switchto;
	ActiveAnimation->Play(true);
	ActiveAnimation->SetPause(false);
}

void Player::SetCheckPointData()
{
	this->checkpointPosition.x = (long)positionX;
	this->checkpointPosition.y = (long)positionY;
}

void Player::ResetToCheckPoint()
{
	isCrushing = 0.0f;
	//Deactivate tools
	if(toolManager->GetIsGrapple())
		toolManager->ToggleTool(GRAPPLE);
	//if(toolManager->GetIsThermo())
	//{
	//	toolManager->ToggleTool(THERMO);

	//}
	if(toolManager->GetIsMagnet())
		toolManager->ToggleTool(MAGNET);
	//Reset player's position
	this->positionX = (float)checkpointPosition.x;
	this->positionY = (float)checkpointPosition.y;

	//TODO: reset the position of everything else
}

void Player::SetHitBox(RECT hitBox)
{
	hitBox.left = (LONG)(positionX - widthSolid / 2);
	hitBox.right = (LONG)(positionX + widthSolid / 2);
	hitBox.top = (LONG)(positionY - heightSolid / 2);
	hitBox.bottom = (LONG)(positionY + heightSolid /2) + 10;

	this->hitBox = hitBox;
}